package com.larly.config;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云客户端配置
 */
@ConfigurationProperties(prefix = "aliyun.client")
@Configuration
@Data
@Slf4j
public class AliClientConfig {

    private String accessKeyId;
    private String accessKeySecret;

    @Bean
    public Client AliClient() {
        Config config = new Config()
                // 配置 AccessKey ID，请确保代码运行环境设置了环境变量。
                .setAccessKeyId(accessKeyId)
                // 配置 AccessKey Secret，请确保代码运行环境设置了环境变量。
                .setAccessKeySecret(accessKeySecret);

        // 配置 Endpoint
        config.endpoint = "dysmsapi.aliyuncs.com";

        try {
            return new Client(config);
        } catch (Exception e) {
            log.error("阿里云客户端初始化失败", e);
            throw new RuntimeException(e);
        }
    }
}
